﻿using ImageGallery.Classes;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ImageGallery.Control
{
    /// <summary>
    /// Interaction logic for ImageControl.xaml
    /// </summary>
    public partial class ImageControl : UserControl
    {
        private List<string> mSupportedExtensions;


        public ObservableCollection<ImageClass> Images
        {
            get { return (ObservableCollection<ImageClass>)GetValue(ImagesProperty); }
            set { SetValue(ImagesProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Images.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ImagesProperty =
            DependencyProperty.Register("Images", typeof(ObservableCollection<ImageClass>), typeof(ImageControl), new PropertyMetadata(null));




        public ImageClass SelectedImage
        {
            get { return (ImageClass)GetValue(SelectedImageProperty); }
            set { SetValue(SelectedImageProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SelectedImage.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedImageProperty =
            DependencyProperty.Register("SelectedImage", typeof(ImageClass), typeof(ImageControl), new PropertyMetadata(null));




        public ImageControl()
        {
            InitializeComponent();
            Images = new ObservableCollection<ImageClass>();
            mSupportedExtensions = new List<string>()
            { ".bmp", ".jpg", ".gif", ".jpe", ".jpeg", ".jfif", ".tif", ".tiff" };
            this.DataContext = this;
        }

        public void LoadImages(string pathLocation, bool deepSearch)
        {
            Images.Clear();
            SelectedImage = null;
            SearchOption opt = SearchOption.TopDirectoryOnly;
            if (deepSearch)
                opt = SearchOption.AllDirectories;


            foreach (var file in new DirectoryInfo(pathLocation).GetFiles("*",  opt))
            {
                AddImage(file.FullName);
            }
        }
        public void LoadImages(List<string> imagesLocation)
        {
            Images.Clear();
            SelectedImage = null;

            foreach (var item in imagesLocation)
            {
                if (File.Exists(item))
                {
                    AddImage(item);
                }
            }
        }


        public void AddImage(string location)
        {
            if (!Images.Any(x => x.ImageUri.AbsolutePath == location))
            {
                FileInfo file = new FileInfo(location);
                if (IsImage(file.Extension))
                {
                    ImageClass newImg = new ImageClass(location);
                    Images.Add(newImg);
                }
            }
        }

        private bool IsImage(string extension)
        {
            if (mSupportedExtensions.Contains(extension.ToLower())) return true;
            return false;
        }

        private void border_MouseDown_1(object sender, MouseButtonEventArgs e)
        {
            var image = ((Border)sender).Child;
            if (image is Image)
            {
                ImageClass img = ((Image)image).DataContext as ImageClass;
                SelectedImage = img;
            }
        }
    }
}
